
package com.neiquan.backstage.resourec_manage.webaction;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.BackendErrorCode;
import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.common.util.Sessions;
import com.neiquan.backstage.common.util.Strings;
import com.neiquan.backstage.pub.bsc.dao.po.AdminUser;
import com.neiquan.backstage.pub.bsc.dao.po.Entrepreneur;
import com.neiquan.backstage.resourec_manage.EntrepreneurService;

@Controller
@RequestMapping(value = "allianceEntrepreneur", produces = { "text/json;charset=UTF-8" })
public class EntrepreneurAction {

	private static Class<?> poc = Entrepreneur.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private EntrepreneurService entrepreneurService;

	/**
	 * 返回全部Entrepreneur已审核成功的
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "allEntrepreneurStatus1")
	public String allEntrepreneurStatus1(HttpSession session) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<Entrepreneur> list = (List<Entrepreneur>) extraSpringHibernateTemplate.getHibernateTemplate().find("From Entrepreneur where status=? and memberId=?", 1, memberId);
		return CommonResp.buildSuccessResp(list).toJsonString();
	}

	/**
	 * 通过公司名字查询memberId
	 * 
	 * @return group by memberId List<Entrepreneur>
	 */
	@ResponseBody
	@RequestMapping(value = "allEntrepreneur")
	public String allEntrepreneur(String name) {
		Entrepreneur entityE = (Entrepreneur) extraSpringHibernateTemplate.findAllByPropEq(poc, "name", name);
		Integer memberId = entityE.getMemberId();
		List<Entrepreneur> list = (List<Entrepreneur>) extraSpringHibernateTemplate.getHibernateTemplate().find("From Entrepreneur where status=? and memberId=?", 1, memberId);
		return CommonResp.buildSuccessResp(list).toJsonString();
	}

	/**
	 * 添加Entrepreneur
	 */
	@ResponseBody
	@RequestMapping(value = { "addEntrepreneur" })
	public String addStation(String name, String loge, String description, String linkman, String phone, String mail, String address, HttpSession session, String projectType, String projectDesc, Integer teamSize) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		if (entrepreneurService.queryByName(Strings.trimAndNotNull(name), memberId) != null) {
			return CommonResp.buildErrorResp(BackendErrorCode.already_exist).toJsonString();
		}
		Entrepreneur entity = new Entrepreneur();
		entity.setName(Strings.trimAndNotNull(name));
		entity.setLoge(Strings.trimAndNotNull(loge));
		entity.setDescription(Strings.trimAndNotNull(description));
		entity.setLinkman(Strings.trimAndNotNull(linkman));
		entity.setPhone(Strings.trimAndNotNull(phone));
		entity.setMail(Strings.trimAndNotNull(mail));
		entity.setAddress(Strings.trimAndNotNull(address));
		entity.setProjectType(Strings.trimAndNotNull(projectType));
		entity.setProjectDesc(Strings.trimAndNotNull(projectDesc));
		entity.setTeamSize(teamSize);
		entity.setSaveTime(System.currentTimeMillis());
		entity.setStatus(0);
		entity.setMemberId(memberId);
		entrepreneurService.add(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	/**
	 * 修改entrepreneur
	 */
	@ResponseBody
	@RequestMapping(value = { "edit" })
	public String edit(int id, String name, String loge, String description, String linkman, String phone, String mail, String address, String projectType, String projectDesc, Integer teamSize) {
		Entrepreneur entity = entrepreneurService.queryById(id);
		if (entity == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		if (!entity.getName().equals(name) && entrepreneurService.queryByName(Strings.trimAndNotNull(name), entity.getMemberId()) != null) {
			return CommonResp.buildErrorResp("公司名称已存在").toJsonString();
		}
		entity.setName(Strings.trimAndNotNull(name));
		entity.setLoge(Strings.trimAndNotNull(loge));
		entity.setDescription(Strings.trimAndNotNull(description));
		entity.setLinkman(Strings.trimAndNotNull(linkman));
		entity.setPhone(Strings.trimAndNotNull(phone));
		entity.setMail(Strings.trimAndNotNull(mail));
		entity.setAddress(Strings.trimAndNotNull(address));
		entity.setProjectType(Strings.trimAndNotNull(projectType));
		entity.setProjectDesc(Strings.trimAndNotNull(projectDesc));
		entity.setTeamSize(teamSize);
		entrepreneurService.updata(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}

}
